﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class UserOperLogForm : Form
    {
        SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
        SqlDataAdapter sda = new SqlDataAdapter();

        public UserOperLogForm()
        {
            InitializeComponent();
        }

        private void UserLoginLogForm_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable da = new DataTable();
                string strSql = "select time, userID, username, opertype, state, monitorDev_Name, device_Name, Note from tb_useroper";
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(da);
                BindingSource bindingSource1 = new BindingSource();
                bindingSource1.DataSource = da;

                this.dataGridView1.DataSource = bindingSource1;
                this.bindingNavigator1.BindingSource = bindingSource1;

                this.dataGridView1.Columns[0].HeaderText = "时间";
                this.dataGridView1.Columns[1].HeaderText = "用户号码";
                this.dataGridView1.Columns[2].HeaderText = "用户姓名";
                this.dataGridView1.Columns[3].HeaderText = "操作类型";
                this.dataGridView1.Columns[4].HeaderText = "状态";
                this.dataGridView1.Columns[5].HeaderText = "监控箱设备号";
                this.dataGridView1.Columns[6].HeaderText = "传感器设备号";
                this.dataGridView1.Columns[7].HeaderText = "详细";
                this.dataGridView1.Columns[7].Width = 333;

                this.dataGridView1.Columns[0].ReadOnly = true;
                this.dataGridView1.Columns[1].ReadOnly = true;
                this.dataGridView1.Columns[2].ReadOnly = true;
                this.dataGridView1.Columns[3].ReadOnly = true;
                this.dataGridView1.Columns[4].ReadOnly = true;
                this.dataGridView1.Columns[5].ReadOnly = true;
                this.dataGridView1.Columns[6].ReadOnly = true;
                this.dataGridView1.Columns[7].ReadOnly = true;

                this.dateTimePicker1.Value = DateTime.Now;
                this.dateTimePicker2.Value = DateTime.Now;
                this.checkBox1.Checked = false;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private string buildsqlstring()
        {
            string strSql = "select time, userid, username, opertype, state , monitorDev_Name, device_Name, Note from tb_useroper where item > 0 ";
            try
            {
               
                if (this.textBox1.Text != "")
                {
                    strSql += " and userid = '" + this.textBox1.Text + @"'";
                }
                if (this.textBox2.Text != "")
                {
                    strSql += " and " + @"username = '" + this.textBox2.Text + @"'";
                }
                if (this.comboBox1.SelectedItem != null)
                {
                    if (this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString() != "全部")
                    {
                        strSql += " and " + @"opertype = '" + this.comboBox1.Items[this.comboBox1.SelectedIndex] + @"'";
                    }
                }
                if (this.checkBox1.Checked)
                {
                    DateTime starttime = this.dateTimePicker1.Value.Date;
                    DateTime endtime = this.dateTimePicker2.Value.Date;

                    endtime = endtime.AddHours(23);
                    endtime = endtime.AddMinutes(59);
                    endtime = endtime.AddMinutes(59);

                    if (starttime <= endtime)
                    {
                        strSql += " and " + @"time >=  '" + starttime.ToShortDateString() + @"' and  " + @"time <=  '" + endtime.ToShortDateString() + @"'";
                    }
                    else
                    {
                        MessageBox.Show("开始时间必须小于结束时间");
                        return null;
                    }
                }
         //       MessageBox.Show(strSql);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            return strSql;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable da = new DataTable();
                string strSql = this.buildsqlstring();
                if (strSql == null)
                {
                    return;
                }
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(da);
                BindingSource bindingSource1 = new BindingSource();
                bindingSource1.DataSource = da;
                this.dataGridView1.DataSource = bindingSource1;
                this.bindingNavigator1.BindingSource = bindingSource1;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}
